#include "viml/function.h"
#include "viml/value.h"
#include <stdexcept>

namespace viml {

Function::Function(const std::string& n, 
                   std::vector<std::string> params,
                   std::shared_ptr<Bytecode> code,
                   size_t start_addr)
    : name(n), parameters(std::move(params)), 
      bytecode(std::move(code)), start_address(start_addr) {}

size_t Function::arity() const {
    return parameters.size();
}

Closure::Closure(std::shared_ptr<Function> func)
    : function(std::move(func)) {}

void Closure::capture(const std::string& name, const Value& value) {
    captured_variables[name] = value;
}

bool Closure::has_captured(const std::string& name) const {
    return captured_variables.find(name) != captured_variables.end();
}

Value Closure::get_captured(const std::string& name) const {
    auto it = captured_variables.find(name);
    if (it == captured_variables.end()) {
        throw std::runtime_error("Captured variable not found: " + name);
    }
    return it->second;
}

} // namespace viml